package com.jky.trial.controller;

import com.jky.trial.annotation.NoRepeatSubmit;
import com.jky.trial.common.BaseResult;
import com.jky.trial.common.Constants;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.Record;
import com.jky.trial.entity.User;
import com.jky.trial.service.RecordService;
import com.jky.trial.vo.RecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/v1/record")
public class RecordController {

    private RecordService recordService;

    @Autowired
    RecordController(RecordService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    @NoRepeatSubmit
    public BaseResult create(@RequestBody Record record, HttpServletRequest request) {
        String id = JWTUtil.getId(request.getHeader("tk"));
        record.setUserId(Integer.valueOf(id));//记录人
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }
    @PostMapping("huiCi")
    @NoRepeatSubmit
    public BaseResult createHuiCi(@RequestBody RecordVo record, HttpServletRequest request) {
        String id = JWTUtil.getId(request.getHeader("tk"));
        record.setUserId(Integer.valueOf(id));//记录人
        recordService.createHuiCi(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping("huiCi")
    @NoRepeatSubmit
    public BaseResult updateHuiCi(@RequestBody RecordVo record, HttpServletRequest request) {
        String id = JWTUtil.getId(request.getHeader("tk"));
        record.setUserId(Integer.valueOf(id));//记录人
        recordService.updateHuiCi(record);
        return new BaseResult(true, record.getId());
    }
    @GetMapping("detail/{id}")
    public BaseResult detailProcessHuiCi(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @PutMapping
    public BaseResult update(@RequestBody Record record) {
        return new BaseResult(true, recordService.update(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping("/qutu/{id}")
    public BaseResult qutu(@PathVariable Integer id) {
        return new BaseResult(true, recordService.quTuDetail(id));
    }

    @GetMapping
    public ListResult page(RecordVo record ,HttpServletRequest request) {
        return recordService.page(record);
    }

    /**
     *  @param record.type
     *  @param record.targetId
     * @param record
     * @param request
     * @return
     */
    @GetMapping("/selectMaxDeep")
    public BaseResult selectMaxDeep(RecordVo record ,HttpServletRequest request) {
        Float lastDepth = recordService.selectMaxDeep(record);
        return new BaseResult(true,lastDepth==null?0:lastDepth);
    }

    /**
     * 按项目ID获取土样
     * @param record
     * @param request
     * @return
     */
    @GetMapping("/project/{projectId}")
    public ListResult projectPage(RecordVo record ,HttpServletRequest request) {
        return recordService.projectPage(record);
    }

    @GetMapping("/list")
    public ListResult list(RecordVo record) {
        return new ListResult(true, recordService.list(record));
    }
}
